use crate::dto::*;
use crate::entity::*;
use crate::RB;
use hypers::prelude::{hypers_openapi, openapi, OpenApi, Responder, Response};
use log::error;
use rbatis::plugin::page::PageRequest;
use rbatis::rbdc::datetime::DateTime;

pub struct Banner;

#[openapi(
    prefix = "/api",
    components(schemas(BannerSaveReq, BannerDeleteReq, BannerUpdateReq, BannerListReq))
)]
impl Banner {
    #[post("banner_save",tag = "添加app轮播图",request_body = BannerSaveReq)]
    async fn banner_save(item: BannerSaveReq) -> impl Responder {
        log::info!("banner_save params: {:?}", &item);
        let banner = InterviewBanner {
            id: None,
            title: item.title,
            image_url: item.image_url,
            webview_url: item.webview_url,
            banner_sort: item.banner_sort,
            banner_status: item.banner_status,
            remark: item.remark,
            create_time: Some(DateTime::now()),
            update_time: Some(DateTime::now()),
        };
        let result = InterviewBanner::insert(&mut RB.clone(), &banner).await;
        handle_result(result)
    }

    #[post("banner_delete",tag = "删除app轮播图",request_body = BannerDeleteReq)]
    async fn banner_delete(item: BannerDeleteReq) -> impl Responder {
        log::info!("banner_delete params: {:?}", &item);
        let result = InterviewBanner::delete_in_column(&mut RB.clone(), "id", &item.ids).await;
        handle_result(result)
    }

    #[post("banner_delete",tag = "更新app轮播图",request_body = BannerUpdateReq)]
    async fn banner_update(item: BannerUpdateReq) -> impl Responder {
        log::info!("banner_update params: {:?}", &item);

        let banner = InterviewBanner {
            id: item.id,
            title: item.title,
            image_url: item.image_url,
            webview_url: item.webview_url,
            banner_sort: item.banner_sort,
            banner_status: item.banner_status,
            remark: item.remark,
            create_time: None,
            update_time: Some(DateTime::now()),
        };
        let result = InterviewBanner::update_by_column(&mut RB.clone(), &banner, "id").await;
        handle_result(result)
    }

    #[post("banner_list",tag = "查询app轮播图",request_body = BannerListReq)]
    async fn banner_list(item: BannerListReq) -> impl Responder {
        log::info!("banner_list params: {:?}", &item);

        let page = &PageRequest::new(item.page_no, item.page_size);
        let result = InterviewBanner::select_page(&mut RB.clone(), page).await;
        let mut res = Response::default();
        match result {
            Ok(d) => {
                let mut banner_list_data: Vec<BannerListData> = Vec::new();
                for x in d.records {
                    banner_list_data.push(BannerListData {
                        id: x.id,
                        title: x.title,
                        image_url: x.image_url,
                        webview_url: x.webview_url,
                        banner_sort: x.banner_sort,
                        banner_status: x.banner_status,
                        remark: x.remark.unwrap_or_default(),
                        create_time: x.create_time.unwrap().0.to_string(),
                        update_time: x.update_time.unwrap().0.to_string(),
                    })
                }
                res.json(&ok_result_page(banner_list_data, d.total));
                res
            }
            Err(err) => {
                error!("{}", err.to_string());
                res.json(&err_result_page(err.to_string()));
                res
            }
        }
    }
}
